define(function(require, exports, module)
{
	var $ = require("$");
	var store = require("store");
	var Header = require("header");
	var Menu = require("menu");
	var bootbox = require("bootbox");

	localStorage.getItem("userData");
	
	exports.init = function(param)
	{
		$.ajax(
		{
			url : GLOBAL.URL.GETUSER, // 获取用户信息
			dataType : "json",
			async : false,
			success : function(data)
			{
				if (data.retFlag)
				{
					var userInfo = data.userInfo;
					localStorage.setItem("userType", userInfo.type);
					/*var userType = userInfo.userType;
					// 普通用户跳转
					if (9 == userType ||3 == userType)
					{
						window.location.href = data.redirecturl;
						return;
					}*/
					var loginTip = "<div class='item-title' style='margin-top: 50px;margin-left: 30px;'>您好，" +
					                userInfo.userName + "，欢迎使用通信能力平台系统。";
					if(userInfo.lastLoginTime != null && userInfo.lastLoginTime != ""){
					  loginTip += "您上次登录的时间是"+userInfo.lastLoginTime +"。</div>";					  
					}else{
					  loginTip += "这是您首次登陆系统，请修改默认登陆密码。</div>";    
					}
					new Header(
					{
						renderTo : param.headerId
					});
					// 通过角色获取菜单
					var menu = new Menu(
					{
						attrs :
						{
							url : GLOBAL.URL.MENU_GETMENUBEAN, // 查询用户所属菜单   
							data : "userId=" + userInfo.userId //"roleIds=" + userInfo.roleIds
						},
						renderTo : param.menuId
					});

					// 主页没有工作台时显示默认图片
					$("#" + param.contentId).html(
					    loginTip +
					    "<div style='bottom:5px; right:5px; position:fixed;'>" +
							"<img src='img/indexbg.png'/>" +
							"</div>");
					var menuData = menu.model;
					var nav = require("bootstrap_nav");
					if (menuData)
					{
						for (var i in menuData)
						{
							var m = menuData[i];
							if ("homepage.html" == m.url)
							{
								nav.toPageView("homepage.html");
								break;
							}
						}
					}
					if (store.enabled)
					{
						store.set("userInfo", userInfo);
					}
					$('#span_currentuser').html(userInfo.userName);
					$("#span_currentuser").attr("userid", userInfo.userId);
					$(".jxsypt_help").remove();

					/*// 显示经营主体或运营商
					if (userInfo.carrierName != null)
					{
						$("#nav_toggler").after("<div class='pull-left' style='margin-left:30px;'><h5><font color='white' style='font-family:Microsoft YaHei'>" + userInfo.carrierName + "</font></h5></div>");
					}*/
					$(".noadmin").attr("style", "display:none");
					$(".navbar-search").attr("style", "display:none");
				}
				else
				{
				  oauth2Relogin();
					/*//检查是否需要重新登录
					if (data.retRelogin) {
						//重新登录
						oauth2Relogin();
						return;
					}
					bootbox.alert(data.retMsg);*/
				}
				
				if (localStorage.getItem("contrast") == null)
				{
					localStorage.setItem("contrast", "contrast-red");
				}
				if (localStorage.getItem("content") !== null)
				{
					$("#color-settings-body-color").attr("href", localStorage.getItem("content"));
				}
				if ((localStorage.getItem("contrast") !== null) && !$("body").hasClass("contrast-background"))
				{
					$("body")[0].className = $("body")[0].className.replace(/(^|\s)contrast.*?(\s|$)/g, " ").replace(/\s\s+/g, " ").replace(/(^\s|\s$)/g, "");
					$("body").addClass(localStorage.getItem("contrast"));
				}
				$(".color-settings-body-color > a").hover(function()
				{
					$("#color-settings-body-color").attr("href", $(this).data("change-to"));
					return localStorage.setItem("content", $(this).data("change-to"));
				});
				return $(".color-settings-contrast-color > a").hover(function()
				{
					$('body')[0].className = $('body')[0].className.replace(/(^|\s)contrast.*?(\s|$)/g, ' ').replace(/\s\s+/g, ' ').replace(/(^\s|\s$)/g, '');
					$('body').addClass($(this).data("change-to"));
					return localStorage.setItem("contrast", $(this).data("change-to"));
				});
			},
			error : function()
			{
				bootbox.alert("请求失败，请联系管理员！");
			}
		});
	};
});
